import config from '@/common/config.js';
import Utils from '@/common/utils.js';

import pinia from '@/store/index.js';
import { usePiniaStore } from '@/store/pinia.js';
const Pinia = usePiniaStore(pinia);


class common {
	//  自定义跳转路径
	PageUrl(item) {
		if (!item.type) return;
		let id = item.classify_id || item.id;
		if (item.type == 4) { // 文章列表
			Utils.toUrl(`/pages/List/List?listId=${id}`)
		}
		if (item.type == 5) { // 文章详情页
			Utils.toUrl(`/User/introduce?id=${id}`)
		}

		if (item.type == 15) { // 视频号
			if (item.website_list_type == 13) { // 视频号首页
				wx.openChannelsUserProfile({
					finderUserName: item.finderUserName,
				})
			}
			if (item.website_list_type == 14) { // 视频号视频
				wx.openChannelsActivity({
					finderUserName: item.finderUserName,
					feedId: item.feedId
				})
			}
			if (item.website_list_type == 15) { // 视频号直播
				wx.openChannelsLive({
					finderUserName: item.finderUserName
				})
			}
			if (item.website_list_type == 16) { // 视频号活动页
				wx.openChannelsEvent({
					finderUserName: item.finderUserName,
					eventId: item.eventId
				})
			}
		}
		if (item.type == 16) { // 打电话
			uni.makePhoneCall({
				phoneNumber: item.website_list_url
			})
		}
		if (item.type == 17) { // 导航
			uni.openLocation({
				latitude: Number.parseFloat(item.seatData.lat),
				longitude: Number.parseFloat(item.seatData.lng),
				address: item.website_list_url,
				success(res) {
					console.log('打开成功');
				},
				fail(err) {
					console.log(err);
				}
			})
		}
		if (item.type == 18) { // 调查问卷
			wx.openEmbeddedMiniProgram({
				appId: item.appID,
				path: item.website_list_url,
			});
		}
		if (item.type == 20) { // 分类

			Utils.toUrl(`/pages/EventsClass/EventsClass?cate_id=${id}`)
		}
		if (item.type == 21) { // 活动详情
			Utils.toUrl(`/pages/EventsDetails/EventsDetails?event_id=${id}`)
		}

	}


	//  支付
	getPayment(item) {
		return new Promise((resolve, reject) => {
			uni.requestPayment({
				provider: 'wxpay',
				orderInfo: item,
				timeStamp: item.timeStamp,
				nonceStr: item.nonceStr,
				package: item.package,
				signType: item.signType,
				paySign: item.paySign,
				success: (res) => {
					resolve(res)
				},
				fail: (err) => {
					reject(err);
				}
			})
		})
	}

	async chooseImage(count) {
		return new Promise((resolve, reject) => {
			uni.chooseImage({
				count,
				success: async (res) => {
					const tempFilePaths = res.tempFilePaths;
					let arr = [];
					for (const item of tempFilePaths) {
						let result = await this.uploadFiles(item);
						let imgItem = result.url.substring(22, result.url.length);
						arr.push(imgItem)
					}
					resolve(arr);
				}
			})
		})
	}

	uploadFiles(file) {
		return new Promise((resolve, reject) => {
			const uploadTask = uni.uploadFile({
				url: config.baseUrl + `/zzjApp/uploade?bid=${Pinia.bid}`,
				filePath: file,
				name: 'file',
				dataType: "json",
				success: function(uploadFileRes) {
					if (uploadFileRes.data) {
						if (typeof uploadFileRes.data != 'object') {
							uploadFileRes.data = JSON.parse(uploadFileRes.data)
						}
						resolve(uploadFileRes.data);
					}

				},
				fail: function(res) {
					console.log(res)
				},

			})
		})
	}


	// 图片上传
	uploadFile(file) {
		return new Promise((resolve, reject) => {
			const uploadTask = uni.uploadFile({
				url: config.baseUrl + `/zzjApp/uploade?bid=${Pinia.bid}`,
				filePath: file,
				name: 'file',
				dataType: "json",
				success: function(uploadFileRes) {
					if (uploadFileRes.data) {
						if (typeof uploadFileRes.data != 'object') {
							uploadFileRes.data = JSON.parse(uploadFileRes.data)
						}
						resolve(uploadFileRes.data);
					}

				},
				fail: function(res) {
					console.log(res)
				},

			})
		})
	}
}

export default new common();